package org.market.domain.strategy.service.rule.tree.impl;

import lombok.extern.slf4j.Slf4j;
import org.market.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import org.market.domain.strategy.service.rule.tree.IRuleTreeNode;
import org.market.domain.strategy.service.rule.tree.factory.DefaultRuleTreeFactory;
import org.market.types.common.Constants;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @Author: Insight
 * @Description: TODO
 * @Date: 2025/2/25 下午10:05
 * @Version: 1.0
 */
@Slf4j
@Service
@Component("rule_luck")
public class RuleTreeLuckNode implements IRuleTreeNode {
    @Override
    public DefaultRuleTreeFactory.RuleActionEntity logic(String userId, Long strategyId, Integer awardId,String ruleValue) {

        String[] split = ruleValue.split(Constants.COLON);
        if (split.length == 0){
            log.error("规则过滤-兜底奖品，兜底奖品未配置告警 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);
            throw new RuntimeException("兜底奖品未配置 " + ruleValue);
        }

        Integer luckAwardId = Integer.valueOf(split[0]);
        String awardRuleValue = split.length > 1 ? split[1] : "";

        log.info("规则过滤-兜底奖品 userId:{} strategyId:{} awardId:{} awardRuleValue:{}", userId, strategyId, luckAwardId, awardRuleValue);
        return DefaultRuleTreeFactory.RuleActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .strategyAwardVO(DefaultRuleTreeFactory.StrategyAwardVO.builder()
                        .awardId(luckAwardId)
                        .awardRuleValue(awardRuleValue)
                        .build())
                .build();
    }
}
